function Hand(){
	this.cards = new Array(2);
	this.getValue = getValue;
	this.getName = getName;
	this.getCards = getCards;
	this.getBestCards = getBestCards;
	this.getHand = getHand;
	this.bestCards = null;
	this.initialized = false; // kas käsi on juba ära hinnatud?
}
// tagastame parima käe nime
function getName(){
	if (!this.initialized){
		this.bestCards = this.getBestCards();
		this.initialized = true;
	}
	return this.bestCards.identify().name;
}
// tagastame parima käe väärtuse
function getValue(){
	if (!this.initialized){
		this.bestCards = this.getBestCards();
		this.initialized = true;
	}
	return this.bestCards.identify().rank;
}
// tagastame parima käe kaardid
function getCards(){
	if (!this.initialized){
		this.bestCards = this.getBestCards();
		this.initialized = true;
	}
	var cards = "";
	for ( var i = 0; i < this.bestCards.cards.length; i++) {
		cards += this.bestCards.cards[i].value + this.bestCards.cards[i].suit[0] + " ";
	}
	return cards;
}
// tagastame parima käe objekti
function getHand(){
	if (!this.initialized){
		this.bestCards = this.getBestCards();
		this.initialized = true;
	}
	return this.bestCards;
}

// leiame parima käe
function getBestCards(){
	ccs = game.cCards.slice(0);
	ccs.push(this.cards[0]);
	ccs.push(this.cards[1]);
	var handCombinations = combinations(ccs);
	var max = -1;
	var bestHands = [];
	
	for (var i=0; i < handCombinations.length; i++) {
		var rank = handCombinations[i].identify().rank;
		if (rank > max){
			max = rank;
			bestHands = [];
			bestHands.push(handCombinations[i]);
		}
		else if(rank == max) {
			bestHands.push(handCombinations[i]);
		}
	}
	if (bestHands.length == 1){
		return bestHands[0];
	}
	else {
		//leia parim paaridest, kolmikutest vms
		return getBestHand(bestHands);
	}
}
// if h1 is less than h2    -> return -1
// if h1 is equal to h2     -> return 0
// if h1 is greater than h2 -> return 1
function getBestHand(hands){
	var best = hands[0];
	for ( var i = 1; i < hands.length; i++) {
		if (gaga.compareHands(best,hands[i]) === -1){
			best = hands[i];
		}
	}
	return best;
}

// leiame kombinatsioonid 7st 5 kaupa
function combinations(ccs){
	var hands = [];
	for(var i = 0; i < 3; i++) {
			for(var j = i + 1; j < 4; j++) {
				for(var k = j + 1; k < 5; k++) {
					for(var l = k + 1; l < 6; l++) {
						for(var m = l + 1; m < 7; m++) {
							hands.push(gaga.createHand([
								[ccs[i].rank[0], ccs[i].suit[0].toLowerCase()],
								[ccs[j].rank[0], ccs[j].suit[0].toLowerCase()],
								[ccs[k].rank[0], ccs[k].suit[0].toLowerCase()],
								[ccs[l].rank[0], ccs[l].suit[0].toLowerCase()],
								[ccs[m].rank[0], ccs[m].suit[0].toLowerCase()]
								]));
						}
					}
				}
			}
		}
	return hands;
}
